//
//  HRZhengQuanViewController.m
//  Economical report
//
//  Created by wyzc on 16/6/23.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CELLID @"CELL"
#import "HRZhengQuanViewController.h"
#import "HRZhengQuanModel.h"
#import "HRZhengQuanCell.h"

@interface HRZhengQuanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArr;
@end

@implementation HRZhengQuanViewController
-(NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr==nil) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self sendRequeu];
    self.tableView.rowHeight=88;
    UINib *nib=[UINib nibWithNibName:@"HRZhengQuanCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CELLID];
}
//发送网络请求
-(void)sendRequeu
{
    NSURLSession *session=[NSURLSession sharedSession];
    NSURL *url=[NSURL URLWithString:@"http://api.21jingji.com/news/getList?subcat=759&page=1"];
    NSURLSessionDataTask *dataTask=[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *catNameDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSArray *arr=catNameDict[@"list"];
        for (NSDictionary *dict in arr) {
            HRZhengQuanModel *model=[HRZhengQuanModel modelWithDict:dict];
            [self.dataSourceArr addObject:model];
        }
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.tableView reloadData];
        }];
        
    }];
    [dataTask resume];
}

#pragma mark--tableView数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRZhengQuanCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID];
    HRZhengQuanModel *model=self.dataSourceArr[indexPath.row];
    cell.model=model;
    return cell;
    
}



@end
